<?php

namespace app\controller;

use app\BaseController;


class Position extends Base
{

    public function initialize()
    {
        parent::initialize();
        Token::apisecurity();
    }

    /**
     * 职位列表
     * api post /position/index
     * @param int id 职位ID
     * @param int department_id 部门ID
     * @return json
     */
    public function index()
    {
        if (_isget()) {
            if ((isset(_get()['id']) && _get()['id'])) {
                $where = [];
                $where[] = ['delete_time', '=', 0];
                $where[] = ['id', '=', _get()['id']];
                $result = db('position', 'mysql')->where($where)->find();
                if (!$result) {
                    _Error('职位不存在');
                }
                $result['create_time'] = totime($result['create_time']);
                $result['update_time'] = totime($result['update_time']);
                _Success('职位详情', 200, $result);
            } else {
                $where = [];
                $where[] = ['delete_time', '=', 0];
                if (isset(_get()['department_id']) && _get()['department_id']) {
                    $where[] = ['department_id', '=', _get()['department_id']];
                }
                $list = db('position', 'mysql')->where($where)->select()->each(function ($item) {
                    $item['create_time'] = totime($item['create_time']);
                    $item['update_time'] = totime($item['update_time']);
                    return $item;
                })->toArray();
                _Success('职位列表', 200, list_to_tree($list, 'id', 'parent_id'));
            }
        }
        _Result('响应失败', 500);
    }

    /**
     * 添加职位
     * api post /position/position_add
     * @param string position_name 职位名称
     * @param string position_desc 职位描述
     * @param int department_id 部门ID
     * @param int parent_id 上级职位ID
     * @return json
     */
    public function position_add()
    {
        if (_ispost()) {
            if (!isset(_post()['position_name']) || !_post()['position_name']) {
                _Error('职位名称未填写');
            }
            if (!isset(_post()['department_id']) || !_post()['department_id']) {
                _Error('部门未选择');
            }
            if (!db('department', 'mysql')->where('id', _post()['department_id'])->find()) {
                _Error('选择的部门不存在');
            }
            if (isset(_post()['parent_id']) && _post()['parent_id']) {
                if (!db('position', 'mysql')->where('id', _post()['parent_id'])->find()) {
                    _Error('选择的职位不存在');
                }
            }

            $data = _post();
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = db('position', 'mysql')->cache('getPositionSelect')->insert($data);
            if (!$result) {
                _Error('添加失败');
            }
            return _Success('添加成功');
        }
        _Result('响应失败', 500);
    }

    /**
     * 更新职位
     * api post /position/position_edit
     * @param int id 职位ID
     * @param string position_name 职位名称
     * @param string position_desc 职位描述
     * @return json
     */
    public function position_edit()
    {
        if (_ispost()) {
            if (!isset(_get()['id']) || !_get()['id']) {
                _Error('职位主键未提交');
            }
            if (!isset(_post()['department_id']) || !_post()['department_id']) {
                _Error('部门未选择');
            }
            if (!db('department', 'mysql')->where('id', _post()['department_id'])->find()) {
                _Error('选择的部门不存在');
            }
            if (isset(_post()['parent_id']) && _post()['parent_id']) {
                if (!db('position', 'mysql')->where('id', _post()['parent_id'])->find()) {
                    _Error('选择的职位不存在');
                }
            }
            $data = _post();
            $data['update_time'] = time();
            $result = db('position', 'mysql')->cache('getPositionSelect')->where('id', $data['id'])->update($data);
            if (!$result) {
                _Error('更新失败');
            }
            return _Success('更新成功');
        }
        _Result('响应失败', 500);
    }

    /**
     * 删除职位
     * @param int id 职位ID
     */
    public function position_del()
    {
        if (_ispost()) {
            if (!isset(_post()['id']) || !_post()['id']) {
                _Error('职位主键未提交');
            }
            if (db('users', 'mysql')->where('position_id', 'like', "%," . _post()['id'] . ",%")->find()) {
                _Error('职位下有员工信息');
            }
            if (db('position', 'mysql')->where('parent_id', _post()['id'])->find()) {
                _Error('职位下有次级职位');
            }
            $result = db('position', 'mysql')->where('id', _post()['id'])->cache('getPositionSelect')->delete();
            if (!$result) {
                _Error('删除失败');
            }
            return _Success('删除成功');
        }
        _Result('响应失败', 500);
    }
}
